package com.aizhixin.lab.docker.common;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
public class ExceptionDockerHandle {
    /**
     * @param e
     * @return 验证异常
     */
    @ResponseStatus(value = HttpStatus.OK)
    @ExceptionHandler(ExceptionValidation.class)
    public  Map<String,Object> doRequiredExceptionHandle(ExceptionValidation e) {
        Map<String,Object> result=new HashMap<>();
        result.put("result",Boolean.FALSE);
        result.put("code",e.getCode());
        result.put("cause",e.getMessage());
        return result;
    }

    @ResponseStatus(value = HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(ExceptionAuth.class)
    public Map<String,Object> doRequiredExceptionHandle(ExceptionAuth e) {
        Map<String,Object> result=new HashMap<>();
        result.put("result",Boolean.FALSE);
        result.put("code",e.getCode());
        result.put("cause",e.getMessage());
        return result;
    }

    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    @ExceptionHandler(Exception.class)
    public Map<String,Object> doRequiredExceptionHandle(Exception e) {
        e.printStackTrace();
        Map<String,Object> result=new HashMap<>();
        result.put("result",Boolean.FALSE);
        result.put("code",400);
        result.put("cause","服务器错误");
        return result;
    }
}
